//
//  QScanInforMationVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/23.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QScanInforMationVC.h"
#import "QScanInforMationView.h"
#import "QPayPlayBoxView.h"
#import "QSetUpPassWordVC.h"
#import "QPaySuccessVC.h"
#import "QForgetPayPassWordVC.h"
@interface QScanInforMationVC ()

@end

@implementation QScanInforMationVC{

    UIControl *_maskView;
    QPayPlayBoxView *_playView;
    NSString *_passWordStr;
    
    QScanInforMationView *_headView;
    NSDictionary *_dataDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫确认订单信息";
    [self createUI];
    [self createPayView];
    [self requestData:_sId];//加满订单详情
}

- (void)createUI{
    
    WS(wSelf);
    _headView = [[[NSBundle mainBundle] loadNibNamed:@"QScanInforMationView" owner:self options:nil] lastObject];
    _headView.layer.shadowColor = [QColorUtility defaultLowGrayColor].CGColor;
    _headView.layer.shadowOpacity = 1;
    _headView.layer.shadowOffset = CGSizeMake(0, 3);
    _headView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    _headView.layer.cornerRadius = 4;
    [self.view addSubview:_headView];
    
    CGFloat bottom = 0;
    if(iphone4x_3_5){
        bottom = -80;
    }else if (iphone5x_4_0){
        bottom = -120;
    }else if (iphone6_4_7){
        bottom = -150;
    }else if (iphone6Plus_5_5){
        bottom = -180;
    }
    
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset((kNavHeight+kStatusHeight)+25);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(bottom);
        make.left.equalTo(wSelf.view.mas_left).offset(20);
        make.right.equalTo(wSelf.view.mas_right).offset(-20);
    }];
    
    
    UIButton *_agreeButton = [QViewUtility createButtonWithFrame:CGRectZero font:17 title:@"确认支付" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    _agreeButton.tag = 4;
    _agreeButton.layer.cornerRadius = 5.0;
    _agreeButton.layer.shadowColor = [QColorUtility defaultBlueColor].CGColor;
    _agreeButton.layer.shadowOpacity = 1;
    _agreeButton.layer.shadowOffset = CGSizeMake(0, 2);
    [_agreeButton addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_agreeButton];
    
    CGFloat btnBottom = 0.0;
    if(iphone4x_3_5){
        btnBottom = -20;
    }else if (iphone5x_4_0){
        btnBottom = -40;
    }else if (iphone6_4_7){
        btnBottom = -50;
    }else if (iphone6Plus_5_5){
        btnBottom = -60;
    }
    
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(btnBottom);
        make.left.equalTo(wSelf.view.mas_left).offset(30);
        make.right.equalTo(wSelf.view.mas_right).offset(-30);
        make.height.equalTo(@45);
    }];
}


- (void)btnAction{
    CLog(@"确定支付");
    if([[QUserInfo sharedInfo].loginModel.isPayPassword isEqualToString:@"1"]){
        [self playPayView];
    }else{
        QSetUpPassWordVC *vc = [[QSetUpPassWordVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//加满订单详情
- (void)requestData:(NSString *)sid{
    NSString *url = [QParamService urlWithAction:@"orderActualRefuelDetail"];
    NSDictionary *param = [QParamService fullOrderId:sid];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            _dataDic = basModel.items;
            _headView.dic = _dataDic;
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark -- 初始化支付框
- (void)createPayView{
    __weak QScanInforMationVC *wself = self;
    _maskView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [_maskView addTarget:self action:@selector(maskAction) forControlEvents:UIControlEventTouchUpInside];
    _maskView.hidden = YES;
    [self.view addSubview:_maskView];
    
    _playView = [[QPayPlayBoxView alloc] init];
    [_maskView addSubview:_playView];
    
    _playView.StrBlock = ^(NSString *str){
        __strong QScanInforMationVC *sSelf = wself;
        CLog(@"str====%@",str);
        sSelf->_passWordStr = str;
    };
    _playView.NotiBlock = ^(NSInteger tag){
        CLog(@"确定支付");
        
        if(tag == 1){
            __strong QScanInforMationVC *sSelf = wself;
            QForgetPayPassWordVC *vc = [[QForgetPayPassWordVC alloc] initWithNibName:@"QForgetPayPassWordVC" bundle:nil];
            [sSelf.navigationController pushViewController:vc animated:YES];
            [sSelf maskAction];
        }else if (tag == 2){
            __strong QScanInforMationVC *sSelf = wself;
            if(sSelf->_passWordStr.length < 4){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"支付密码错误,请重新输入" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
                return ;
            }
            [sSelf maskAction];
            NSString *orderId = sSelf->_dataDic[@"orderId"];
            if(orderId.length > 0){
                [sSelf payMoneyWithOrderId:orderId];
            }
        }
    };
}

#pragma mark -- 弹出支付框
- (void)playPayView{
    _maskView.hidden = NO;
    _playView.payMoneyStr = _dataDic[@"totalprice"];
    _playView.yuMoney = [QUserInfo sharedInfo].loginModel.balance;
    _maskView.alpha = 0;
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        _maskView.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}

#pragma mark -- 隐藏支付框
- (void)maskAction{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    _maskView.alpha = 1;
    [UIView animateWithDuration:.3 animations:^{
        _maskView.alpha = 0;
    } completion:^(BOOL finished) {
        _maskView.hidden = YES;
    }];
}

#pragma mark--支付接口
- (void)payMoneyWithOrderId:(NSString *)orderId{
    NSString *url = [QParamService urlWithAction:@"submitOrder"];
    if(_passWordStr.length > 0){
        NSString *md5Str = [QMD5Hash md5String:_passWordStr];
        md5Str = [md5Str uppercaseString];
        _passWordStr = md5Str;
    }
    NSDictionary *param = [QParamService payWithorderId:orderId paymentStatus:@"4" payPassWord:_passWordStr draweeTel:[QUserInfo sharedInfo].loginModel.lastOrderPhone couponCode:@""];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        _passWordStr = @"";
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QUserInfo sharedInfo].payTypeStr = payorder;
            QPaySuccessVC *vc = [[QPaySuccessVC alloc] initWithNibName:@"QPaySuccessVC" bundle:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

@end
